package damon;


import damon.common.ConstantObject;
import damon.swing.About;
import damon.swing.ReadJPanel;
import damon.swing.SplashScreen;
import damon.swing.WriteJPanel;
import damon.utils.OpcWindowUtil;

import javax.swing.*;
import java.awt.*;

/**
 * @author Akshathkumar Shetty
 */
public class OpcPointRun extends JFrame {
    private static final String CLIENT = "c";
    private static final String SERVER = "s";
    private static final String UDP = "u";
    private ClassLoader cl = getClass().getClassLoader();
    public ImageIcon logo = new ImageIcon(cl.getResource("icons/tree.jpeg"));
    public ImageIcon ball = new ImageIcon(cl.getResource("icons/ball1.gif"));
    private JTabbedPane tabbedPane;

//    private static Map<String, NetService> services = new HashMap<>();

    /**
     * Creates a new instance of SocketTest
     */
    public OpcPointRun() {
//        Container cp = getContentPane();

        tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        ReadJPanel client = new ReadJPanel(this);
        WriteJPanel writeClient = new WriteJPanel(this);
//        SocketTestServer server = new SocketTestServer(this);
//        SocketTestUdp udp = new SocketTestUdp(this);
        About about = new About();

//        services.put(CLIENT, client);
//        services.put(SERVER, server);
//        services.put(UDP, udp);

        tabbedPane.addTab("Read", ball, (Component) client, "Read opc point");
        tabbedPane.addTab("Write", ball, (Component) writeClient, "Write opc point");
//        tabbedPane.addTab("Server", ball, server, "Test any client");
//        tabbedPane.addTab("Udp", ball, udp, "Test any UDP Client or Server");
        tabbedPane.addTab("About", ball, about, "About SocketTest");

        tabbedPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        this.add(tabbedPane);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("net.sourceforge.mlf.metouia.MetouiaLookAndFeel");
        } catch (Exception e) {
            //e.printStackTrace();
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception ee) {
                System.out.println("Error setting native LAF: " + ee);
            }
        }

        boolean toSplash = true;
//        if (args.length > 0) {
//            if ("nosplash".equals(args[0])) toSplash = false;
//        }

        SplashScreen splash = null;
        if (toSplash) splash = new SplashScreen();

        OpcPointRun st = new OpcPointRun();
        st.setTitle("OpcPoint v "+ConstantObject.VERSION);
        st.setSize(650, 500);
        OpcWindowUtil.centerWindow(st);
        st.setDefaultCloseOperation(EXIT_ON_CLOSE);
        st.setIconImage(st.logo.getImage());

        setUpArgParams(args);

        if (toSplash) splash.kill();
        st.setVisible(true);
    }

    private static void setUpArgParams(String[] configuration) {
        if (configuration.length < 1) {
            return;
        }

        //c:ip:port
        //s:ip:port
        //u:ip:port
//        for (String arg : configuration) {
//            String[] params = arg.split(":");
//            if (params.length == 3) {
//                String service = params[0];
//                String ip = params[1];
//                String port = params[2];
//                services.get(service).setUpConfiguration(ip, port);
//            }
//        }
    }

}


